package com.beerandjesus.sportofkings.handicapper.strategy.brisnet.singlefile;

import java.util.List;
import java.math.BigDecimal;

import com.beerandjesus.sportofkings.handicapper.model.RaceAnalysisResultVOImpl;
import com.beerandjesus.sportofkings.handicapper.model.ResultsTableImpl;
import com.beerandjesus.sportofkings.handicapper.model.ResultType;
import com.beerandjesus.sportofkings.handicapper.model.MixedNumeral;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Race;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Workout;
import com.beerandjesus.sportofkings.handicapper.strategy.RacingCalculator;
import org.apache.commons.lang.StringUtils;

public abstract class AbstractRaceAnalysisStrategy {

	protected static final Float ZERO = 0f;
	protected static final Float ELEVEN = new Float(11.0);
	protected static final String SIRE_TRAINER_WORKOUTS = "STW";
	protected static final String STARTS = "STARTS";
	protected static final String SPEED = "SPEED";
	protected static final String Q_PTS = "Q PTS";
	protected static final String UP_CLOSE = "UP CLOSE";
	protected static final String W_OFF_PACE = "W OFF PACE";
	protected static final String WPS = "WPS";
	protected static final String WORKS = "WORKS";
	protected static final String NAME = "NAME";
	protected static final String MORNING_LINE = "M/L";
	protected static final String CLASS_ALL = "CLASS ALL";
	protected static final String CLASS_L2 = "CLASS L2";
	protected static final String AVG_PURSE_L2 = "AVG PURSE L2";
	protected static final String AUCTION_PRICE_STUD_FEE = "ACTN/STUD";

	protected RacingCalculator calc = new RacingCalculator();
	private FastWorkoutResolver workResolver = new FastWorkoutResolver();

    public AbstractRaceAnalysisStrategy() {
		super();
	}
	
	protected Integer getInteger(String value, Integer theDefault) {
		try {
			return new Integer(value);
		} catch (Exception e) {
			return theDefault;
		}
		
	}

	protected int getSharpWorkouts(Horse h, StringBuffer works) {
		int sharpCount = 0;
		for (Workout w : h.getWorkouts()) {
			if (workResolver.isSharpWorkout(w)) {
				sharpCount++;
				if (works.length() > 0) {
					works.append(",");
				}
				works.append(calc.distanceFromYardsShortNotation(w.getDistance()));
			}
		}
		return sharpCount;
	}

	protected void setCommonFields(RaceAnalysisResultVOImpl vo, Race r, List<Horse> horses) {
		vo.setFieldCount(horses.size());
		vo.setPurse(r.getPurse());
		vo.setClaimingPrice(r.getClaimingPrice());
		vo.setRaceName(r.getRaceClassification());
		vo.setRaceType(r.getType());
		vo.setSpeedPar(r.getBrisSpeedPar());
		vo.setSurface(r.getSurface());
		vo.setTrackCode(r.getTrack());
		vo.setRaceDate(r.getDate());
		vo.setRaceNumber(r.getRaceNumber());
		vo.setPostTimes(r.getPostTimes());
		vo.setDistance(calc.distanceFromYards(r.getDistance()));
	}

	public Float getFloatOrZero(String value) {
		try {
			return new Float(value.trim());
		} catch (Exception e) {
			return ZERO;
		}
	}

    protected boolean equal(String value, int i) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        try {
            return (i == new Integer(value));
        } catch (NumberFormatException e) {
            return false;
        }
    }

    protected boolean greaterThan(String value, int i) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        try {
            return (new Integer(value) > i);
        } catch (NumberFormatException e) {
            return false;
        }
    }
    protected void morningLineOdds(Horse h, ResultsTableImpl table, Integer pp, ResultType type) {
        String odds;
        if (StringUtils.isEmpty(h.getMorningLineOdds())) {
            odds = "";
        } else {
            MixedNumeral mlOdds = calc.toFraction(new BigDecimal(h.getMorningLineOdds()));
            odds = mlOdds.toImproperFractionString();
        }
        table.putValue(pp, type, MORNING_LINE, odds);
    }
}